package com.Cychat.pet.controller;

import com.Cychat.commons.entity.ResultMessage;
import com.Cychat.commons.entity.pojo.Pet;
import com.Cychat.commons.entity.pojo.PetOwner;
import com.Cychat.pet.service.PetOwnerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.Cychat.pet.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Auther: 谢宝辉  <br/>
 * Date: 2025/10/30:17:41  <br/>
 * Description: 宠物控制器
 */
@RestController
@RequestMapping("/api/pet")
@CrossOrigin
public class PetController {

    @Autowired
    private PetService petService;

    @Autowired
    private PetOwnerService petOwnerService;

    /**
     * Description : 根据用户id查询到宠物主人id
     * ChangeLog : 1. 创建 (\ 12:17 [Lenovo]);
     * @auther: 谢宝辉
     * date: \:12:17
     * @param userId 用户ID
     * @return com.Cychat.commons.entity.ResultMessage<java.lang.Integer>
    */
     @GetMapping("/ownerIdByUserId/{userId}")
    public ResultMessage<Integer> getPetOwnerIdByUserId(@PathVariable("userId") Long userId) {
        Integer petOwnerIdByUserId = petService.getPetOwnerIdByUserId(userId);
        if (petOwnerIdByUserId == null) {
            return ResultMessage.error("用户没有绑定宠物主人");
        }
        return ResultMessage.success(petOwnerIdByUserId);
    }


    /**
     * Description : 根据用户ID查询到宠物主人id再查询宠物列表
     * ChangeLog : 1. 创建 (2025/10/29 10:21 [Lenovo]);
     * @auther: 谢宝辉
     * date: 2025/10/29:10:21
     * @param userId
     * @return com.Cychat.commons.entity.ResultMessage<java.util.List<com.Cychat.commons.entity.pojo.Pet>>
     */
    @GetMapping("/user/{userId}")
    public ResultMessage<List<Pet>> getPetsByUserId(@PathVariable("userId") Long userId) {
        // 查询该用户是否为宠物主人
        Integer petOwnerIdByUserId = petService.getPetOwnerIdByUserId(userId);
        if (petOwnerIdByUserId == null) {
            // 新增宠物主人
            PetOwner petOwner = new PetOwner();
            petOwner.setUserId(userId);
            petOwnerService.save(petOwner);
            // 保存后需要获取新生成的petOwnerId
            petOwnerIdByUserId = petOwner.getOwnerId(); // 假设这是主键字段名
        }

        List<Pet> petsByOwnerId = petService.getPetsByOwnerId(petOwnerIdByUserId);
        return ResultMessage.success(petsByOwnerId);
    }
    /**
     * Description : 根据宠物ID查询宠物详情
     * ChangeLog : 1. 创建 (2025/10/29 10:21 [Lenovo]);
     * @param petId
     * @return com.Cychat.commons.entity.pojo.Pet
     * @auther: 谢宝辉
     * date: 2025/10/29:10:21
     */
    @GetMapping("/getPetById/{petId}/{userId}")
    public ResultMessage<Pet> getPetById(@PathVariable("petId") Integer petId, @PathVariable("userId") Long userId) {
        Pet pet = petService.getById(petId);
        if (pet == null) {
            return ResultMessage.error("宠物不存在");
        }
        return ResultMessage.success(pet);
    }

    /**
     * Description : 新增宠物
     * ChangeLog : 1. 创建 (2025/10/31 14:14 [Lenovo]);
     * @auther: 谢宝辉
     * date: 2025/10/31:14:14
     * @param pet 宠物对象
     * @return com.Cychat.commons.entity.ResultMessage<com.Cychat.commons.entity.pojo.Pet>
    */
    @PostMapping("/addPet/{userId}")
    public ResultMessage<Pet> addPet(@PathVariable("userId") Long userId, @RequestBody Pet pet) {
        //新增时间
        pet.setCreateTime(new Date());
        pet.setUpdateTime(new Date());
        boolean save = petService.save(pet);
        if (!save) {
            return ResultMessage.error("新增宠物失败");
        }
        return ResultMessage.success(pet);
    }

    /**
     * Description : 修改宠物
     * ChangeLog : 1. 创建 (2025/11/3 15:02 [Lenovo]);
     * @auther: 谢宝辉
     * date: 2025/11/3:15:02
     * @param pet 宠物对象
     * @return null
    */
    @PostMapping("/updatePet/{userId}")
    public ResultMessage<Pet> updatePet(@PathVariable("userId") Long userId, @RequestBody Pet pet) {
        //修改时间
        pet.setUpdateTime(new Date());
        boolean update = petService.updateById(pet);
        if (!update) {
            return ResultMessage.error("修改宠物失败");
        }
        return ResultMessage.success(pet);
    }


}
